package com.itaem.community.core.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author tomato
 */

@Getter
@NoArgsConstructor
@ApiModel("分页数据实体")
public class PageData<T> {

    /**
     * 分页数据
     */
    @ApiModelProperty("分页数据")
    private Collection<T> records;

    /**
     * 请求页码/当前页码
     */
    @ApiModelProperty("请求页码/当前页码")
    private Long currentPage;

    /**
     * 当前数据量大小
     */
    @ApiModelProperty("当前页数据量大小")
    private Long pageSize;

    /**
     * 总页码数
     */
    @ApiModelProperty("总页码数")
    private Long totalPages;

    /**
     * 总数据量
     */
    @ApiModelProperty("总数据量")
    private Long totalCount;

    public PageData(Collection<T> records, Long currentPage, Long pageSize, Long totalPages, Long totalCount) {
        this.records = records;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalPages = totalPages;
        this.totalCount = totalCount;
    }

    public <R> PageData<R> map(Function<? super T, ? extends R> mapper) {
        return new PageData<>(
                this.records.stream().map(mapper).collect(Collectors.toList()),
                this.currentPage,
                this.pageSize,
                this.totalPages,
                this.totalCount
        );
    }
}
